package com.lalifa.googledate.ui.activity

import android.graphics.drawable.GradientDrawable
import androidx.core.view.isVisible
import com.drake.brv.BindingAdapter
import com.lalifa.base.BaseActivity
import com.lalifa.ext.showTipDialog
import com.lalifa.extension.PATTERN_DATE
import com.lalifa.extension.PATTERN_HOUR_MINUTE
import com.lalifa.extension.format
import com.lalifa.extension.gone
import com.lalifa.extension.hideKeyboard
import com.lalifa.extension.onClick
import com.lalifa.extension.start
import com.lalifa.extension.text
import com.lalifa.extension.toast
import com.lalifa.extension.visible
import com.lalifa.googledate.R
import com.lalifa.googledate.api.EventListBean
import com.lalifa.googledate.api.HolidayUp
import com.lalifa.googledate.databinding.ActivitySearchBinding
import com.lalifa.googledate.ui.adapter.searchAdapter
import com.lalifa.googledate.util.CalendarUtils
import com.lalifa.googledate.util.CalendarUtils.CALENDARS_Id
import com.lalifa.googledate.util.CalendarUtils.deleteCalendarEvent
import com.lalifa.utils.LiveDataBus
import com.lalifa.utils.UIKit
import org.joda.time.LocalDate

class SearchActivity : BaseActivity<ActivitySearchBinding>() {
    override fun getViewBinding() = ActivitySearchBinding.inflate(layoutInflater)
    private var adapter: BindingAdapter? = null
    private var eventInfo: EventListBean? = null
    override fun initView() {
        binding.apply {
            adapter = list.searchAdapter().apply {
                R.id.item_event_name.onClick {
                    adapter.models = arrayListOf()
                    info.visible()
                    hideKeyboard(etSearch)
                    val model = getModel<EventListBean>()
                    eventInfo =model
                    val eventInfo = model.event
                    eventName.text = if (eventInfo.accountname.contains("diy#"))
                        eventInfo.accountname.split("#")[1]
                    else model.event.accountname
                    eventRange.text = eventInfo.title
                    eventAddress.isVisible = !eventInfo.address.isNullOrEmpty()
                    eventAddress.text = eventInfo.address.pk("")
                    if (!eventInfo.isallday) {
                        val p: String =
                            eventInfo.starttime.format(PATTERN_DATE)
                        val start: String =
                            eventInfo.starttime.format(PATTERN_HOUR_MINUTE)
                        val end: String =
                            eventInfo.endtime.format(PATTERN_HOUR_MINUTE)
                        val ss = "$p $start-$end"
                        eventTime.text = ss
                    } else {
                        val localDate = LocalDate(eventInfo.starttime)
                        val today: LocalDate = LocalDate.now()
                        if (localDate.year == today.year) {
                            val range = localDate.toString("MMM-dd")
                            eventTime.text = range
                        } else {
                            val range = localDate.toString("MMM-dd, YYYY")
                            eventTime.text = range
                        }
                    }
                    holiday.text = eventInfo.info
                    val shape = GradientDrawable()
                    shape.cornerRadius = resources.getDimensionPixelSize(R.dimen.fourdp).toFloat()
                    shape.setColor(eventInfo.eventcolor)
                    roundRect.background = shape
                }
            }
        }
    }

    override fun onClick() {
        super.onClick()
        binding.apply {
            close.onClick { info.gone() }
            back.onClick {
                finish()
            }
            editBtn.onClick {
                info.gone()
                start(AddActivity::class.java) {
                    putExtra("EventListBean", eventInfo)
                }
            }
            delBtn.onClick {
                showTipDialog(
                    getString(R.string.del_sure), getString(R.string.cancel),
                    getString(R.string.sure)
                ) {
                    getProgress(this@SearchActivity)
                    deleteCalendarEvent(eventInfo!!.event.id.toLong()) {
                        if (it==0) {
                            LiveDataBus.get().with("holidayList").value = HolidayUp(-1)
                            UIKit.postDelayed({
                                hindProgress()
                                info.gone()
                                finish()
                            }, 1000)

                        }

                    }
                }
            }
            sure.onClick {
                val text = etSearch.text()
                if (text.isEmpty()) {
                    toast(getString(R.string.place_input_name))
                    return@onClick
                }
                val filter = MainActivity.eventList.filter {
                    it.event.title.contains(text)&&it.event.accountname== CalendarUtils.CALENDARS_NAME }
                if (filter.isEmpty()) {
                    noData.visible()
                } else {
                    noData.gone()
                    adapter!!.models = filter
                }
            }
        }
    }
}